﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ErrorCodeConvert
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private string JudgeCodeType(string codeType)
        {
            string codeTypeActualy = "E";
            switch (codeType) {
                case "1":codeTypeActualy = "A";break;
                case "2": codeTypeActualy = "E"; break;
                case "3": codeTypeActualy = "H"; break;
                case "4": codeTypeActualy = "P"; break;
                case "5": codeTypeActualy = "M"; break;
                case "6": codeTypeActualy = "C"; break;
            }
            return codeTypeActualy;
        }

        private string JudgeCodeTypeToNumber(string codeType)
        {
            string codeTypeActualy = "1";
            switch (codeType)
            {
                case "A": codeTypeActualy = "1"; break;
                case "E": codeTypeActualy = "2"; break;
                case "H": codeTypeActualy = "3"; break;
                case "P": codeTypeActualy = "4"; break;
                case "M": codeTypeActualy = "5"; break;
                case "C": codeTypeActualy = "6"; break;
            }
            return codeTypeActualy;
        }

        private string JudgeCode(string code)
        {
            string codeActualy = "";
            if (code.Length > 3)
            {
                string temp = code.Substring(0, 2);
                ASCIIEncoding asciiEncoding = new ASCIIEncoding();
                byte[] byteArray = new byte[] { (byte)(int.Parse(temp)+ 55) };
                string strCharacter = asciiEncoding.GetString(byteArray);
                codeActualy = strCharacter + code.Substring(2);
            }
            else
            {
                codeActualy = code.PadLeft(3, '0');
            }
            return codeActualy;
        }

        private string JudgeCodeToNumber(string code)
        {
            string codeActualy = "";
            if (Asc2(code.Substring(0,1))>64 && Asc2(code.Substring(0, 1)) < 91)
            {
                //A:10,B:11...
                codeActualy = (Asc2(code.Substring(0, 1)) - 55).ToString() + code.Substring(1);
            }
            else
            {
                codeActualy = code;
            }
            return codeActualy;
        }

        public static int Asc2(string character)
        {
            if (character.Length == 1)
            {
                ASCIIEncoding asciiEncoding = new ASCIIEncoding();
                int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
                return (intAsciiCode);
            }
            else
            {
                throw new Exception("无效字母");
            }
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            if (value1.Text.Length < 4)
            {
                return;
            }
            int transmissionlValue = Convert.ToInt32(value1.Text);
            string transmissionlValueX = transmissionlValue.ToString("X");
            string codeType = transmissionlValueX.Substring(0,1);
            string code = transmissionlValueX.Substring(1);
            int codeValue = int.Parse(code, System.Globalization.NumberStyles.HexNumber);           
            value2.Text = JudgeCodeType(codeType)+ JudgeCode(codeValue.ToString());
        }
        
        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            if (value3.Text.Length < 4)
            {
                return;
            }
            string actualyCodeType = JudgeCodeTypeToNumber(value3.Text.Substring(0,1));
            string actualyCode = JudgeCodeToNumber(value3.Text.Substring(1));
            string actualyCodeX = int.Parse(actualyCode).ToString("X");
            actualyCodeX = actualyCodeX.PadLeft(3, '0');
            value4.Text = int.Parse(actualyCodeType + actualyCodeX, System.Globalization.NumberStyles.HexNumber).ToString();
        }

        private void Value1_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[^0-9]");
            e.Handled = re.IsMatch(e.Text);
        }

        private void Value3_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex(@"^[^A-Z0-9]+$");
            e.Handled = re.IsMatch(e.Text);
        }
    }
}
